/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fiap.exemplo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author pf0591
 */
@WebServlet(urlPatterns = "/controler")
public class UsoDaSessao extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String invalidarSessao = req.getParameter("invalidarSessao");
        String historico = "";
        
        if (invalidarSessao != null) {
            session.invalidate();

        } else {
            historico = (String) session.getAttribute("historico");
            String mensagemDigitada = req.getParameter("textoDigitado");


            if (historico == null ) {
                if (mensagemDigitada != null ){
                    historico = mensagemDigitada + "";
                } else {
                    historico = "";
                }
            } else {
                historico += mensagemDigitada;
            }

            session.setAttribute("historico", historico);
        }

        String HTML = montarHTML(historico);
        resp.getWriter().println(HTML);
    }

    private String montarHTML(String historico) {
        String html = "<html><body>";
        html += "<form action='./controler' method='GET'>";

        html += "<input type='checkbox' value='sim' name='invalidarSessao'>";
        html += "<textarea name='textoDigitado' ></textarea>";

        html += "<input type='submit' value='enviar' >";
        html += "<textarea name='textoHistorico' > "
                + historico
                + "</textarea>";

        html += "</form></body></html>";
        return html;
    }
}
